package com.fmblzf.library.edit;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by Administrator on 2015/10/15.
 */
public class EditNumberAddReduce extends EditText {

    /**
     * 接口对应 左右图标的点击监听事件
     */
    public interface Listener {
        //左图标监听事件
        public void onLeftImgOnClick();
        //右图标监听事件
        public void onRightImgOnClick();
    }
    //左右图标的宽度
    private int[] leftRightImageWidth = {0,0};
    //上下文
    private Context context;
    //画线的画笔
    private Paint linepaint = new Paint();
    //图片文字颜色
    private String imgTextColor = "#797979";
    //接口监听
    private Listener listener = null;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public EditNumberAddReduce(Context context) {
        this(context, null, 0);
    }

    public EditNumberAddReduce(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public EditNumberAddReduce(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initWidget();
    }

    /**
     * 初始化控件
     */
    public void initWidget(){
        //触发事件弹出软键盘
//        this.requestFocus();
//        InputMethodManager imm = (InputMethodManager)this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.toggleSoftInput(0,InputMethodManager.SHOW_FORCED);
        //获取左右图标
        Drawable[] drawables = getCompoundDrawables();
        //左图标
        if(drawables[0] != null){
            leftRightImageWidth[0] = drawables[0].getIntrinsicWidth();
        }
        //右图标
        if(drawables[2] != null){
            leftRightImageWidth[1] = drawables[2].getIntrinsicWidth();
        }
        setSelection(getText().length());
        linepaint.setColor(Color.parseColor("#d3d3d3"));
        linepaint.setStrokeWidth(2);
        linepaint.setAntiAlias(true);
        linepaint.setStyle(Paint.Style.FILL);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(leftRightImageWidth[0] ==0 && leftRightImageWidth[1] == 0)
            return super.onTouchEvent(event);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(leftRightImageWidth[0]!=0 && event.getX() <= leftRightImageWidth[0]){
                    //点击左图标的区域，执行该事件
                    Toast.makeText(context,"点击左图标的区域",Toast.LENGTH_LONG).show();
                    if(listener != null)
                        listener.onLeftImgOnClick();
                    return false;//拦截事件，继续向下执行，避免弹出键盘
                }
                if (leftRightImageWidth[1] != 0 && event.getX() >= getWidth() - leftRightImageWidth[1]){
                    //点击右图标的区域，执行该事件
                    Toast.makeText(context,"点击右图标的区域",Toast.LENGTH_LONG).show();
                    if(listener != null)
                        listener.onRightImgOnClick();
                    return false;//拦截事件，继续向下执行，避免弹出键盘
                }
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //left 线
        canvas.drawLine(1,1,1,getMeasuredHeight(),linepaint);
        //top 线
        canvas.drawLine(1,1,getMeasuredWidth(),1,linepaint);
        //right 线
        canvas.drawLine(getMeasuredWidth()-1,1,getMeasuredWidth()-1,getMeasuredHeight(),linepaint);
        //bottom 线
        canvas.drawLine(1,getMeasuredHeight()-1,getMeasuredWidth()-1,getMeasuredHeight()-1,linepaint);

        //两条竖线
        canvas.drawLine(leftRightImageWidth[0],0,leftRightImageWidth[0],getMeasuredHeight(),linepaint);
        canvas.drawLine(getMeasuredWidth() - leftRightImageWidth[1], 0, getMeasuredWidth() - leftRightImageWidth[1], getMeasuredHeight(), linepaint);

//        canvas.restore();

        super.onDraw(canvas);
    }
}
